package com.itfreer.gis.server.utils;

import com.itfreer.form.api.dictionary.DictionaryEntity;
import com.itfreer.form.api.dictionary.DictionaryService;

import java.util.*;

public class MapDataUtils {

    private static String defID = "g_data_definition";
    private static String mapID = "g_custom_service_config";

    /**
     * 服务定义ID -> 数据定义ID -> 数据表名
     *
     * @param id
     * @param dictionaryManage
     * @param defID
     * @return
     */
    public static TableInfo getMapInfo(String mapName, DictionaryService dictionaryManage) {
        List<DictionaryEntity> dics = dictionaryManage.getAll(mapID);
        for (DictionaryEntity dic : dics) {
            // 服务定义名称相同
            if (dic.getCode().equals(mapName)) {
                // 数据定义ID
                String metedata = dic.getOther().get("metedata").toString();
                return getTableInfo(metedata, dictionaryManage);
            }
        }
        return null;
    }

    /**
     * 获取查询的的表名
     *
     * @param metedata
     * @param dictionaryManage
     * @param defID
     * @return
     */
    public static TableInfo getTableInfo(String metedata, DictionaryService dictionaryManage) {
        List<DictionaryEntity> dics = dictionaryManage.getAll(defID);
        TableInfo tableInfo = null;
        for (DictionaryEntity dic : dics) {
            if (dic.getId().equals(metedata)) {
                tableInfo = new TableInfo();
                tableInfo.tableName = dic.getAbbreviation();
                tableInfo.primaryKeyName = dic.getInfo();
                tableInfo.tableType = dic.getUnits();
                break;
            }
        }
        return tableInfo;
    }

    /**
     * 数据表名 -> 数据定义ID -> 服务定义标识
     *
     * @param tableName
     * @param dictionaryManage
     * @return
     */
    public static String getServiceInfo(String tableName, DictionaryService dictionaryManage) {
        List<DictionaryEntity> dics = dictionaryManage.getAll(defID);
        for (DictionaryEntity dic : dics) {
            if (dic.getAbbreviation().equals(tableName)) {
                String dataDefinitionId = dic.getId();
                String serviceName = getServiceCode(dataDefinitionId, dictionaryManage);

                if (serviceName != null){
                    return serviceName;
                }
            }
        }
        return null;
    }

    /**
     * 获取服务标识
     *
     * @param dataDefinitionId
     * @param dictionaryManage
     * @return
     */
    public static String getServiceCode(String dataDefinitionId, DictionaryService dictionaryManage) {
        List<DictionaryEntity> dics = dictionaryManage.getAll(mapID);
        for (DictionaryEntity dic : dics) {
            // 服务定义元数据id等于数据定义Id
            Map<String, Object> other = dic.getOther();
            if (other.containsKey("metedata")) {
                if (other.get("metedata") != null){
                    String metedata = other.get("metedata").toString();

                    if (metedata.equals(dataDefinitionId)) {
                        return dic.getCode();
                    }
                }
            }

        }
        return null;
    }
}
